﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure;

namespace Demo.TableStorageDataServices.Web
{
    public class WebRole : RoleEntryPoint
    {
        public override bool OnStart()
        {
            if (RoleEnvironment.IsAvailable)
            {
                CloudStorageAccount.SetConfigurationSettingPublisher(
                    (configName, configSetter) =>
                        {
                            configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
                            RoleEnvironment.Changed += (sender, e) =>
                                                           {
                                                               if (e.Changes.OfType<RoleEnvironmentConfigurationSettingChange>().Any(change => change.ConfigurationSettingName == configName))
                                                               {
                                                                   if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
                                                                       RoleEnvironment.RequestRecycle();
                                                               }
                                                           };
                        });
            }
            return base.OnStart();
        }
    }
}